package billwood

import org.springframework.dao.DataIntegrityViolationException

class WoodBillController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        redirect(action: "list", params: params)
    }

    def list(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        [woodBillInstanceList: WoodBill.list(params), woodBillInstanceTotal: WoodBill.count()]
    }

    def create() {
        [woodBillInstance: new WoodBill(params)]
    }

    def save() {
        def woodBillInstance = new WoodBill(params)
        if (!woodBillInstance.save(flush: true)) {
            render(view: "create", model: [woodBillInstance: woodBillInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'woodBill.label', default: 'WoodBill'), woodBillInstance.id])
        redirect(action: "show", id: woodBillInstance.id)
    }

    def show(Long id) {
        def woodBillInstance = WoodBill.get(id)
        if (!woodBillInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'woodBill.label', default: 'WoodBill'), id])
            redirect(action: "list")
            return
        }

        [woodBillInstance: woodBillInstance]
    }

    def edit(Long id) {
        def woodBillInstance = WoodBill.get(id)
        if (!woodBillInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'woodBill.label', default: 'WoodBill'), id])
            redirect(action: "list")
            return
        }

        [woodBillInstance: woodBillInstance]
    }

    def update(Long id, Long version) {
        def woodBillInstance = WoodBill.get(id)
        if (!woodBillInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'woodBill.label', default: 'WoodBill'), id])
            redirect(action: "list")
            return
        }

        if (version != null) {
            if (woodBillInstance.version > version) {
                woodBillInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'woodBill.label', default: 'WoodBill')] as Object[],
                          "Another user has updated this WoodBill while you were editing")
                render(view: "edit", model: [woodBillInstance: woodBillInstance])
                return
            }
        }

        woodBillInstance.properties = params

        if (!woodBillInstance.save(flush: true)) {
            render(view: "edit", model: [woodBillInstance: woodBillInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'woodBill.label', default: 'WoodBill'), woodBillInstance.id])
        redirect(action: "show", id: woodBillInstance.id)
    }

    def delete(Long id) {
        def woodBillInstance = WoodBill.get(id)
        if (!woodBillInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'woodBill.label', default: 'WoodBill'), id])
            redirect(action: "list")
            return
        }

        try {
            woodBillInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'woodBill.label', default: 'WoodBill'), id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'woodBill.label', default: 'WoodBill'), id])
            redirect(action: "show", id: id)
        }
    }
    
def orderWood(){
        
        def total={quanty ->
            BigDecimal mult
            if(quanty<101)
            {
                mult=1
            }
            else
            if((quanty>100)&&(quanty<201))
            {
                mult=2
            }
            else
            if((quanty>200)&&(quanty<301))
            {
                mult=3
            }
            else
            if((quanty>300)&&(quanty<401))
            {
                mult=4
            }
            else
            if((quanty>400)&&(quanty<501))
            {
                mult=5
            }
            else
            {
                mult=5.5
            }
        }
        
        //funtion calculate time procesing the wood       
        def time_procesing={type_wood,quanty->
            BigDecimal total_time
            switch(type_wood)
            {
                case "cherry":
                    total_time=total(quanty)*0.5
                    break
                case "curly Maple":
                    total_time=total(quanty)*1.5
                    break
                case "genuine Mahogany":
                    total_time=total(quanty)*3.0
                    break
                case "wenge":
                    total_time=total(quanty)*5.0
                case "white Oak":
                    total_time=total(quanty)*6.0
                    break                
            }     
        }
        //calculating time timeout wood
        def timeout={hour_cherry,hour_curly_maple,hour_genuine_mahogany,hour_wenge,white_oak ->
            if((hour_cherry>hour_curly_maple) && (hour_cherry>hour_genuine_mahogany)&&(hour_cherry>hour_wenge) && (hour_cherry>white_oak))
            {
                hour_cherry
            }
            else
            if((hour_curly_maple>hour_cherry)&&(hour_curly_maple>hour_genuine_mahogany)&& (hour_curly_maple>hour_wenge) &&(hour_curly_maple> white_oak))
            {
                hour_curly_maple
            }
            else
            if((hour_genuine_mahogany>hour_cherry)&&(hour_genuine_mahogany>hour_curly_maple)&&(hour_genuine_mahogany>hour_wenge)&&(hour_genuine_mahogany>white_oak))
            {
                hour_genuine_mahogany
            }
            else
            if((hour_wenge>hour_cherry)&&(hour_wenge>hour_curly_maple)&&(hour_wenge>hour_genuine_mahogany)&&(hour_wenge>white_oak))
            {
                hour_wenge
            }
            else
            {
                white_oak
            }
        
        }
        
        //calculatng price of wood
        def price={type_wood,quanty->
            switch(type_wood)
            {
                case "cherry":
                    quanty*5.95
                    break
                case "curly maple":
                    quanty*6
                    break
                case "genuine mahogany":
                    quanty*9.6
                    break
                case "wenge":
                    quanty*22.35
                    break
                case "white oak":
                    quanty*6.7
                    break
                case"sawdust":
                    quanty*1.5
                    break
            }   
        }  
        def items=WoodBill.list()
        String name=items.namePerson[0]+"  "+items.lastNamePerson[0]
        String address=items.addressPerson[0]
        BigDecimal TimeCherry=time_procesing("cherry",items.quantyCherry[0])
        BigDecimal TimeCurlyMaple=time_procesing("curly Maple",items.quantyCurlyMaple[0])
        BigDecimal TimeGenuineMahogany=time_procesing("genuine Mahogany",items.quantyGenuineMahogany[0])
        BigDecimal TimeWenge=time_procesing("genuine Mahogany",items.quantyGenuineMahogany[0])
        BigDecimal TimeWhiteOak=time_procesing("white Oak",items.quantyWhiteOak[0])
        BigDecimal PriceCherry=price("cherry",items.quantyCherry[0])
        BigDecimal PriceCurlyMaple=price("curly maple",items.quantyCurlyMaple[0])
        BigDecimal PriceGenuineMahogany=price("genuine mahogany",items.quantyGenuineMahogany[0])
        BigDecimal PriceWenge=price("wenge",items.quantyGenuineMahogany[0])
        BigDecimal PriceWhiteOak=price("white oak",items.quantyWhiteOak[0])
        BigDecimal PriceSawdust=price("sawdust",items.quantySawdust[0])
        BigDecimal PriceTotal=PriceCherry+PriceCurlyMaple+PriceGenuineMahogany+PriceWenge+PriceWhiteOak+PriceSawdust
        BigDecimal TimeTotal=timeout(TimeCherry,TimeCurlyMaple,TimeGenuineMahogany,TimeWenge,TimeWhiteOak)
        [PriceTotal:PriceTotal,address:address,items:items,TimeTotal:TimeTotal,PriceSawdust:PriceSawdust,PriceWhiteOak:PriceWhiteOak,PriceWenge:PriceWenge,PriceGenuineMahogany:PriceGenuineMahogany,PriceCurlyMaple:PriceCurlyMaple,PriceCherry:PriceCherry,name:name,TimeCherry:TimeCherry,TimeCurlyMaple:TimeCurlyMaple,TimeGenuineMahogany:TimeGenuineMahogany,TimeWenge:TimeWenge,TimeWhiteOak:TimeWhiteOak ]
   }
}